<!-- 信用证汇总查询:jzy -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 0px' }">
        <div class="flex">
          <h1 class="f16">信用证汇总查询 双击导出Excel</h1>
        </div>
        <Table
          :tableHeader="tableHeader"
          :tableData="tableData"
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
          @currentRow1="dbClick"
        />
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '0px' }">
        <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
      </el-card>
    </div>

    <el-drawer v-model="drawer2" :direction="'ttb'" size="small" :with-header="false">
      <span @click="drawer = false" style="float: right; cursor: pointer; color: rgb(7, 7, 7)">X</span>
      <div class="flex AIC" style="flex-direction: column">
        <div class="flex mt10 JCC" style="width: 100%; background-color: white">
          <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
            <div class="flex">
              <h1 class="f16"></h1>
              <el-button type="primary" size="default" @click="outExcel">导出</el-button>
            </div>
            <Table :summaryData="summaryData2" summary :tableHeader="tableHeader2" :tableData="tableData2" class="mt10" />
          </el-card>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import router from '@/router'
import { formateData } from '@/plugins/mixinJ'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'SummaryQueryOfLettersOfCredit'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { computed, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

const drawer = ref(false)
const drawer2 = ref(false)
const currentTime = computed(() => {
  const now = new Date()
  const year = now.getFullYear()
  const month = now.getMonth() + 1 // 月份从0开始，需要加1
  const day = now.getDate()
  return `${year}-${month}-${day}`
})

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '信用证号',
    inputValue: ''
  },
  {
    type: 'dateSingle',
    placeholder: '交单日期开始',
    dateLabel: '',
    dateValue: ''
  },
  {
    type: 'dateSingle',
    placeholder: '交单日期结束',
    dateLabel: '',
    dateValue: ''
  }
]) as any

const tableHeader = ref([
  {
    change: true,
    label: '信用证号',
    prop: 'creditNo'
  },
  {
    change: true,
    label: '品牌',
    prop: 'brand'
  },
  {
    label: '交单日期',
    prop: 'deliveryDate'
  },
  {
    width: 110,
    change: true,
    label: '币别',
    prop: 'currency'
  },
  {
    change: true,
    width: 120,
    label: '应收金额',
    prop: 'arAmt'
  },
  {
    change: true,
    label: '备注',
    prop: 'remark'
  }
])
// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '待付金额').selectValue =
      '全部'
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账状态').selectValue =
      '全部'
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async (orderFlag: any = '', orderName: string = '') => {
  isLoading.value = true
  let tableDataList = await service.get<any, any>(
    `/statements/dHFinanceCreditDetail?creditNo=${
      formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '信用证号'
      )?.inputValue
    }&startDate=${formList.value.find((item: any) => item.placeholder === '交单日期开始')?.dateValue}&endDate=${
      formList.value.find((item: any) => item.placeholder === '交单日期结束')?.dateValue
    }&pageSize=${pageSize.value}&pageNum=${currentPage.value}`
  )
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
watch(drawer, () => {
  getTableList()
})
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}
const selectedLists = ref([]) as any
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}
const dbClick = async () => {
  drawer2.value = true
  getTableList2()
}

const tableHeader2 = ref([
  {
    width: '150',
    change: true,
    label: '信用证号',
    prop: 'creditNo'
  },
  {
    change: true,
    label: '类型',
    prop: 'orderType'
  },
  {
    change: true,
    label: '客人发票号',
    prop: 'custInvNo'
  },
  {
    change: true,
    label: '报关发票号',
    prop: 'customsInvNo'
  },
  {
    change: true,
    label: '品牌',
    prop: 'brand'
  },
  {
    label: '出货日期',
    prop: 'outDate'
  },
  {
    label: '出货数量',
    prop: 'outPair'
  },
  {
    label: '币别',
    prop: 'currency'
  },
  {
    label: '加价',
    prop: 'additionalCo'
  },
  {
    label: '出货金额',
    prop: 'outAmt'
  },
  {
    label: '附加费用',
    prop: 'premium'
  },
  {
    width: '150',
    label: '启运港-目的港',
    prop: 'destPort'
  },
  {
    label: '出口类型',
    prop: 'exportType'
  },
  {
    label: '收款类型',
    prop: 'arType'
  },
  {
    label: '应收金额',
    prop: 'arAmt'
  },
  {
    label: '交单日期',
    prop: 'deliveryDate'
  },
  {
    width: 110,
    change: true,
    label: '币别',
    prop: 'currency'
  },
  {
    change: true,
    width: 120,
    label: '实收金额',
    prop: 'recAmt'
  },
  {
    change: true,
    label: '备注',
    prop: 'remark'
  }
])
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData2 = ref<summaryDataType[]>([
  {
    index: 6,
    num: 0
  },
  {
    index: 9,
    num: 0
  },
  {
    index: 14,
    num: 0
  },
  {
    index: 17,
    num: 0
  }
])
const tableData2 = ref([])
const getTableList2 = async () => {
  let sum1 = 0
  let sum2 = 0
  let sum3 = 0
  let sum4 = 0
  const res = (await LoginWork.post(`/statements/getDhFinanceDetail`, currentTableRow.value)) as any
  tableData2.value = res.data
  res.data.forEach((item: any) => {
    sum1 += Number(item.outPair)
    sum2 += Number(item.outAmt)
    sum3 += Number(item.arAmt)
    sum4 += Number(item.recAmt)
  })

  summaryData2.value[0].num = sum1
  summaryData2.value[1].num = sum2
  summaryData2.value[2].num = sum3
  summaryData2.value[3].num = sum4
}
const outExcel = async () => {
  const res = (await fileRequest.post(`/statements/downloadDhFinanceCreditDetail`, currentTableRow.value)) as any
  const link = document.createElement('a') // 利用a标签自定义下载文件名
  let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
  link.style.display = 'none'
  //设置连接
  link.href = window.URL.createObjectURL(blob)
  const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
  link.download = FileName //自定义文件名字
  document.body.appendChild(link)
  //模拟点击事件
  link.click()
  document.body.removeChild(link)
}
</script>
<style lang="less" scoped></style>
